require(["./config"], () => {
    require(["jquery", "request", "template", "header", "nav", "footer"], ($, req, template, Header) => {
        class Category{
            constructor() {
                this.typeId = location.href.match(/\?type=\d{3}/)[0].match(/\d{3}/)[0];
                // console.log(this.typeId);
                this.getUrl = "/lib/json/category-" + this.typeId + ".json";
                // console.log(this.getUrl);
                this.goodsListCurrent = [];
                this.htmlStr = "";
                this.init();
                
            }
            init() {
                this.pageInit()
                    .then(() => {
                        this.jsInit();
                    });
            }
            pageInit() {
                new Header();
                // 获取列表页数据
                return req.categoryData(this.getUrl)
                    .then((res) => {
                        return new Promise((resolve) => {
                            if (res.code === 200){
                                // console.log(res);
                                this.dataCategory = res.data
                                this.goodsListBase = this.dataCategory.data.list;
                                
                                // 第一次数据渲染
                                this.htmlStr = template("categoryTemplate", {data : res.data.data.list});
                                // console.log(this.htmlStr);
                                $("#categoryTemplateBox").html(this.htmlStr);

                                resolve();
                            }
                        })
                    })

            }
            jsInit() {
                const _this = this;
                // 排序设置
                $(".container .sortBox li").click(function () {
                    if (!$(this).hasClass("active")){
                        // console.log("123");
                        $(this)
                        .addClass("active")
                        .siblings()
                        .removeClass("active");

                        let goodsListCurrent = _this.goodsListBase.concat();
                        // console.log(goodsListCurrent);
                        if($(this).index() === 0) {
                            goodsListCurrent = _this.goodsListBase.concat();
                        } else if ($(this).index() === 2) {
                            goodsListCurrent.sort(function (a, b) {return a.spuInfo.skuId - b.spuInfo.skuId})
                        } else if ($(this).index() === 4) {
                            goodsListCurrent.sort(function (a, b) {return a.spuInfo.price - b.spuInfo.price});
                        } else if ($(this).index() === 6) {
                            goodsListCurrent.sort(function (a, b) {return b.spuInfo.price - a.spuInfo.price});
                        }
                        decorateGoods(goodsListCurrent);
                    }
                    // 根据sessionStorage数据进行页面渲染方法
                    function decorateGoods(goodsListCurrent) {
                        const htmlStr = template("categoryTemplate", {data : goodsListCurrent});
                        $("#categoryTemplateBox").html(htmlStr);
                        _this.htmlStr = htmlStr; 
                    }
                })
                

            }
        }
        new Category();
    })
})